package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.product.api.http.model.request.CreateProductHttpRequest;
import com.meituan.catering.management.product.api.http.model.request.SaveProductHttpRequest;
import com.meituan.catering.management.product.biz.model.request.CreateProductBizRequest;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Believe
 * Date 2022/5/23 9:10
 */
public class CreateProductBizRequestConverter {
    public static CreateProductBizRequest toCreateProductBizRequest(CreateProductHttpRequest createProductHttpRequest) {
        if (Objects.isNull(createProductHttpRequest)) {
            return null;
        }
        CreateProductBizRequest createProductBizRequest = new CreateProductBizRequest();
        createProductBizRequest.setName(createProductHttpRequest.getName());
        createProductBizRequest.setUnitPrice(createProductHttpRequest.getUnitPrice());
        createProductBizRequest.setMinSalesQuantity(createProductHttpRequest.getMinSalesQuantity());
        createProductBizRequest.setIncreaseSalesQuantity(createProductHttpRequest.getIncreaseSalesQuantity());
        createProductBizRequest.setUnitOfMeasure(createProductHttpRequest.getUnitOfMeasure());
        createProductBizRequest.setDescription(createProductHttpRequest.getDescription());
        createProductBizRequest.setMethodGroups(toCreateMethodGroup(createProductHttpRequest.getMethodGroups()));
        createProductBizRequest.setAccessoryGroups(toCreateAccessoryGroup(createProductHttpRequest.getAccessoryGroups()));
        return createProductBizRequest;
    }

    public static List<CreateProductBizRequest.AccessoryGroup> toCreateAccessoryGroup(List<CreateProductHttpRequest.AccessoryGroup> accessoryGroupList) {
        if (CollectionUtils.isEmpty(accessoryGroupList)) {
            return Collections.emptyList();
        }
        List<CreateProductBizRequest.AccessoryGroup> accessoryGroups = Lists.newLinkedList();
        for (SaveProductHttpRequest.AccessoryGroup methodGroup : accessoryGroupList) {
            CreateProductBizRequest.AccessoryGroup accessoryGroup = new CreateProductBizRequest.AccessoryGroup();
            accessoryGroup.setName(methodGroup.getName());
            accessoryGroup.setOptions(toAccessoryOption(methodGroup.getOptions()));
            accessoryGroups.add(accessoryGroup);
        }
        return accessoryGroups;
    }

    public static List<CreateProductBizRequest.AccessoryGroup.Option> toAccessoryOption(List<CreateProductHttpRequest.AccessoryGroup.Option> optionList) {
        if (CollectionUtils.isEmpty(optionList)) {
            return Collections.emptyList();
        }
        List<CreateProductBizRequest.AccessoryGroup.Option> optionBOList = Lists.newLinkedList();
        for (SaveProductHttpRequest.AccessoryGroup.Option option : optionList) {
            CreateProductBizRequest.AccessoryGroup.Option optionBiz = new CreateProductBizRequest.AccessoryGroup.Option();
            optionBiz.setName(option.getName());
            optionBiz.setUnitPrice(option.getUnitPrice());
            optionBiz.setUnitOfMeasure(option.getUnitOfMeasure());
            optionBOList.add(optionBiz);
        }
        return optionBOList;
    }

    public static List<CreateProductBizRequest.MethodGroup> toCreateMethodGroup(List<CreateProductHttpRequest.MethodGroup> methodBOList) {
        if (CollectionUtils.isEmpty(methodBOList)) {
            return Collections.emptyList();
        }
        List<CreateProductBizRequest.MethodGroup> methodGroupList = Lists.newLinkedList();
        for (SaveProductHttpRequest.MethodGroup methodGroup : methodBOList) {
            CreateProductBizRequest.MethodGroup methodBizGroup = new CreateProductBizRequest.MethodGroup();
            methodBizGroup.setName(methodGroup.getName());
            methodBizGroup.setOptions(toMethodOption(methodGroup.getOptions()));
            methodGroupList.add(methodBizGroup);
        }
        return methodGroupList;
    }

    public static List<CreateProductBizRequest.MethodGroup.Option> toMethodOption(List<CreateProductHttpRequest.MethodGroup.Option> optionList) {
        if (CollectionUtils.isEmpty(optionList)) {
            return Collections.emptyList();
        }
        List<CreateProductBizRequest.MethodGroup.Option> optionBOList = Lists.newLinkedList();
        for (SaveProductHttpRequest.MethodGroup.Option option : optionList) {
            CreateProductBizRequest.MethodGroup.Option optionBiz = new CreateProductBizRequest.MethodGroup.Option();
            optionBiz.setName(option.getName());
            optionBOList.add(optionBiz);
        }
        return optionBOList;
    }
}
